<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

/**
 * Description of Registion
 *
 * @author SoChishun
 */
class RegistionController {

    public function indexAction() {
        return view('');
    }

    public function doRegistAction() {
        $rule = [
            'username' => 'require|max:30|token|unique:t_user,user_name',
            'password' => 'require|max:30',
            'repassword' => 'require|confirm:password'
        ];
        $message = [
            'username.require' => '用户名未填写',
            'username.max' => '用户名无效',
            'username.unique' => '用户名已存在',
            'username.token' => '表单来源无效',
            'password.require' => '密码未填写',
            'password.max' => '密码无效',
            'repassword.require' => '确认验证码未输入',
            'repassword.confirm' => '两次密码输入不一致'
        ];
        $request = \think\Request::instance();
        $request->only(['username', 'password', 'token']);
        $data = $request->post();
        $validate = \think\Validate::make($rule, $message);
        if (!$validate->check($data)) {
            $error = $validate->getError();
            return ['success' => false, 'message' => $error];
        }
        $user = new \app\index\model\UserModel();
        $user->user_name = $data['username'];
        $user->password = md5($data['password']);        
        if (false===$user->save()) {
            $error = $user->getError();
            return ['success' => false, 'message' => $error];
        }
        return ['success' => true, 'url' => url('login/index')];
    }

}
